import 'package:flutter/material.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/distribution_child_entity.dart';
import 'package:library_base/entities/distribution_entity.dart';
import 'package:library_base/entities/enum_entity.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/res/widget_generator.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/res/edge.dart';
import 'package:get/get.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:auto_size_text/auto_size_text.dart';

Widget buildItem(
  DistributionEntity taskEntity,
  int position, {
  Function? onExpand,
  Function(DistributionEntity taskEntity, int position)? withdraw,
  Function(DistributionChildEntity)? toOrderDetail,
}) {
  return Container(
    color: ThemeUtil.getTheme().colorScheme.secondary,
    width: double.infinity,
    child: Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          alignment: Alignment.centerLeft,
          padding: edgeAll24,
          child: Text(taskEntity.distributionCode, style: normalTextColor28),
        ),
        Container(
          color: ThemeUtil.getTheme().fourthBgColor(),
          padding: edgeV12H24,
          height: 67.r,
          child: Row(
            children: [
              LocalImage(
                'dispatcher_driver',
                package: Constant.packDispatcher(),
                width: 44.r,
                height: 44.r,
              ),
              gapW14,
              Expanded(
                child: Stack(
                  children: [
                    Positioned(
                        bottom: 0,
                        child: LocalImage(
                          'dispatcher_text_bg',
                          package: Constant.packDispatcher(),
                          width: 150.r,
                        )),
                    AutoSizeText(taskEntity.carNo ?? '--',
                        style: const TextStyle(fontSize: 50, fontWeight: FontWeight.bold)),
                  ],
                ),
              ),
              AutoSizeText(
                '¥ ${taskEntity.freightAmt}',
                style: const TextStyle(fontSize: 50, fontWeight: FontWeight.bold),
              ),
            ],
          ),
        ),
        Container(
          padding: edgeLTR24,
          child: Row(crossAxisAlignment: CrossAxisAlignment.center, children: [
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Row(children: [
                    Expanded(
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '${MultiLanguage.driverInformation.tr}：',
                                style: normalTextColor28,
                              ),
                              Expanded(
                                  child: Text(
                                      "${taskEntity.driverName ?? "--"} / ${taskEntity.driverMobile ?? "--"}",
                                      style: normalTextColor28)),
                            ],
                          ),
                          gapH18,
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text('${MultiLanguage.commodityQuantity.tr}：', style: normalTextColor28),
                              Expanded(
                                  child: Text(taskEntity.goodsNumber.toString(),
                                      style: normalTextColor28)),
                            ],
                          ),
                          gapH18,
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text('${MultiLanguage.loadingTime.tr}：', style: normalTextColor28),
                              Expanded(
                                  child: Text(taskEntity.loadingTime, style: normalTextColor28)),
                            ],
                          ),
                        ],
                      ),
                    ),
                    GradientButton(
                      ThemeUtil.getTheme(),
                      decoration: BoxDecoration(
                          borderRadius: const BorderRadius.all(Radius.circular(20)),
                          border: Border.fromBorderSide(
                              BorderSide(color: ThemeUtil.getTheme().colorScheme.primary)),
                          color: ThemeUtil.getTheme().colorScheme.secondary),
                      padding: edgeV20H30 as EdgeInsets,
                      textStyle: transparentTextColor26,
                      text: MultiLanguage.withdraw.tr,
                      normalColor: ThemeUtil.getTheme().colorScheme.secondary,
                      normalTextColor: ThemeUtil.getTheme().colorScheme.primary,
                      onPressed: () {
                        withdraw?.call(taskEntity, position);
                      },
                    ),
                  ]),
                ],
              ),
            ),
          ]),
        ),
        gapH24,
        line1,
        GestureDetector(
          onTap: () {
            taskEntity.isExpand = !taskEntity.isExpand;
            onExpand?.call();
          },
          child: Container(
            color: Colors.transparent,
            width: double.infinity,
            padding: EdgeInsets.symmetric(vertical: 16.r),
            height: 60.r,
            child: LocalImage(
              taskEntity.isExpand ? 'common_expand_close' : 'common_expand_open',
              package: Constant.packLib(),
              width: 30.r,
              height: 30.r,
            ),
          ),
        ),
        Visibility(
            visible: taskEntity.isExpand,
            child: Column(
              children: buildChildItem(taskEntity,toOrderDetail:toOrderDetail),
            ))
      ],
    ),
  );
}

List<Widget> buildChildItem(DistributionEntity taskEntity,{
  Function(DistributionChildEntity)? toOrderDetail
}) {
  return taskEntity.distributionDetailTaskResList.buildItem((index, element) {
    return GestureDetector(onTap: () {}, child: buildChildTaskItem(element, taskEntity,toOrderDetail:toOrderDetail));
  }, dividingLine: () {
    return gapH18;
  }, divideType: DivideType.MIDDLE_WITH_BOTTOM);
}

Widget buildChildTaskItem(
    DistributionChildEntity taskChildEntity,
    DistributionEntity taskEntity,{
      Function(DistributionChildEntity)? toOrderDetail
    }
) {
  return roundWidget(
    width: double.infinity,
    margin: edgeLR24,
    decoration: rs8,
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        GestureDetector(
          onTap: () {
            toOrderDetail?.call(taskChildEntity);
          },
          child: Container(
              padding: edgeAll24,
              color: Colors.transparent,
              child: Row(children: [
                Expanded(
                    child: RichText(
                  text: TextSpan(text: '● ', style: primaryTextColor15, children: [
                    TextSpan(text: taskChildEntity.orderNumber, style: normalTextColor28),
                  ]),
                )),
                LocalImage(
                  'common_black_rightward',
                  package: Constant.packLib(),
                  width: 25.r,
                  height: 25.r,
                )
              ])),
        ),
        line1,
        Container(
          padding: edgeLTR24,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '${MultiLanguage.clientSName.tr}：',
                    style: normalTextColor28,
                  ),
                  Expanded(
                      child: Text(
                          "${taskChildEntity.consignee ?? "--"} / ${taskChildEntity.mobilePhone ?? "--"}",
                          style: normalTextColor28)),
                ],
              ),
              gapH18,
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('${MultiLanguage.shippingAddress.tr}：', style: normalTextColor28with15),
                  Expanded(
                      child: Text(taskChildEntity.detailedaddress ?? "--",
                          style: normalTextColor28with15)),
                ],
              ),
              gapH18,
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('${MultiLanguage.commodityQuantity.tr}：', style: normalTextColor28),
                  Expanded(
                      child:
                          Text(taskChildEntity.goodsNumber.toString(), style: normalTextColor28)),
                ],
              ),
              gapH18,
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('${MultiLanguage.deliveryTime.tr}：', style: normalTextColor28),
                  Expanded(
                      child: Text(taskChildEntity.stationTime ?? '--', style: normalTextColor28)),
                ],
              ),
              gapH18,
            ],
          ),
        ),
      ],
    ),
  );
}
